﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CNDreams.Entity;

using CNDreams.Web.Base;
using Project.Web.Base.Utility;
using CNDreams.Entity.EntitySearch;
using System.ServiceModel;
using System.Net;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Configuration;
using CNDreams.ServiceInterface;
using Serialize.Linq.Extensions;
using Project.Dal.Base;

namespace CNDreams.Web.Models
{
	public class ModelSampleIndex : Model
	{
		private IServiceUTSample bizUTSupplier;
		/// <summary>
		/// 查询实体
		/// </summary>
		private SearchUTSupplier searchEntity;
		public SearchUTSupplier SearchEntity
		{
			get
			{
				if (searchEntity == null)
					return new SearchUTSupplier();
				return searchEntity;
			}
			set
			{
				searchEntity = value;
			}
		}

		/// <summary>
		/// 供应商列表
		/// </summary>
		public IList<UTSample> GridDataSources
		{
			get;
			set;
		}

		public Binding ResolveBinding(string name)
		{
			BindingsSection section = GetBindingsSection(Environment.CurrentDirectory + "web.config");

			foreach (var bindingCollection in section.BindingCollections)
			{
				if (bindingCollection.ConfiguredBindings.Count > 0 && bindingCollection.ConfiguredBindings[0].Name == name)
				{
					var bindingElement = bindingCollection.ConfiguredBindings[0];
					var binding = (Binding)Activator.CreateInstance(bindingCollection.BindingType);
					binding.Name = bindingElement.Name;
					bindingElement.ApplyConfiguration(binding);

					return binding;
				}
			}

			return null;
		}

		private BindingsSection GetBindingsSection(string path)
		{
			System.Configuration.Configuration config =
		  System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(
			  new System.Configuration.ExeConfigurationFileMap() { ExeConfigFilename = path },
				System.Configuration.ConfigurationUserLevel.None);

			var serviceModel = ServiceModelSectionGroup.GetSectionGroup(config);
			return serviceModel.Bindings;
		}

		/// <summary>
		/// 支持多条件查询
		/// </summary>
		public void RetriveData()
		{
			ExpressionCondition<UTSample> condition = ExpressionCondition<UTSample>.GetInstance();
			//IList<string> testCollection = new List<string>() { "aa", "qq" };
			string[] testCollection = new string[] { };
			condition.And(x => (testCollection.Contains(x.ContactName) || x.ContactName != null));

			if (string.IsNullOrEmpty(SearchEntity._CommonSearchCondition))
				condition.And(x => x.SupplierName, SearchEntity._SupplierName, ExpressionValueRelation.Like)
					.And(x => x.SupplyContent, SearchEntity._SupplyContent, ExpressionValueRelation.Like)
					.And(x => x.ContactName, SearchEntity._ContactName, ExpressionValueRelation.Like);
			else
				condition.Or(x => x.SupplierName, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.SupplyContent, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.ContactName, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Mobile, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Telephone, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Email, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Fax, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like)
						.Or(x => x.Address, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like);

			GridDataSources = bizUTSupplier.PaginateListBy(SearchEntity._PageSize.Value, SearchEntity._PageIndex ?? 0, ref Count, condition.ConditionExpression.ToExpressionNode(), SearchEntity._OrderName, SearchEntity.EnumOrderDirection);
			//导出本页数据或者不导出逻辑            
			if (ExportObject.ExportType != null && ExportObject.ExportType == ExportType.导出本页)
			{
				ExportExcel(GridDataSources);
				return;
			}

			//导出全部数据
			if (ExportObject.ExportType != null && ExportObject.ExportType == ExportType.导出全部)
			{
				GridDataSources = bizUTSupplier.ListBy(condition.ConditionExpression.ToExpressionNode(),
				   SearchEntity._OrderName,
				   SearchEntity.EnumOrderDirection);

				ExportExcel(GridDataSources);
				return;
			}

			//分页加载
			PaginateHelperObject = PaginateHelper.ConstructPaginate(SearchEntity._PageSize.Value, Count, SearchEntity._PageIndex ?? 0, SearchEntity, "SearchEntity", HttpContext.Current.Request.RawUrl);

		}

		/// <summary>
		/// 导出成Excel格式
		/// 作者：李方磊
		/// 时间：2011-12-01
		/// </summary>
		/// <param name="datasource">租户合同台账grid数据源</param>
		public void ExportExcel(IList<UTSample> datasource)
		{
			if (ExportObject.ExportType == null)
				return;
			var exportInstance = ExportHelper.GetInstance();
			exportInstance.ExportExcel<UTSample>(datasource, ExportObject, "Sample");
		}

	}


	public class ModelSampleCreate : Model
	{
		private IServiceUTSample bizUTSample;
		/// <summary>
		/// 供应商实体
		/// </summary>
		public UTSample UTSample
		{
			get;
			set;
		}


		/// <summary>
		/// 新增或者修改
		/// </summary>
		/// <param name="sampleCreateModel"></param>
		public void Save()
		{
			bizUTSample.SaveOrUpdate(UTSample);
		}
	}
}